class Solution {
    public static int maxSubarraySumCircular(int[] nums) {
        // 创建 dp 表、初始化、填表、返回值
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];

        f[0] = nums[0];
        g[0] = nums[0];

        for (int i = 1; i < n; i++) {
            f[i] = Math.max(nums[i], f[i-1]+nums[i]);
            g[i] = Math.min(nums[i], g[i-1]+nums[i]);
        }

        int sum = 0, f_max = Integer.MIN_VALUE, g_min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            f_max = Math.max(f_max, f[i]);
            g_min = Math.min(g_min, g[i]);
        }

        return g_min == sum ? f_max : Math.max(f_max, sum - g_min);
    }

    public static void main(String[] args) {
        System.out.println(maxSubarraySumCircular(new int[]{1,-2,3,-2}));
    }
}